package com.fudan.biz.visualize.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fudan.biz.visualize.entity.ChartDef;
import com.fudan.biz.visualize.service.ChartDefService;
import com.fudan.framework.base.DaoService;
import com.fudan.framework.entity.ResponseMsg;
import com.fudan.framework.entity.TableJson;
import com.fudan.framework.utils.EntityUtils;
import com.fudan.framework.web.controller.DeleteController;
import com.fudan.framework.web.controller.FindController;

@Controller
@RequestMapping("biz/chart/def")
public class ChartDefController implements FindController<ChartDef>,DeleteController<ChartDef>{

	@Autowired
	private ChartDefService chartDefService;

	@RequestMapping("/p/{path}")
	public String page(@PathVariable String path) {
		return "biz/chart/"+path;
	}

	@RequestMapping("/save")
	@ResponseBody
	public ResponseMsg save(HttpServletRequest request) {

		try {
			ChartDef chartDef = EntityUtils.toEntity(request, ChartDef.class);
			chartDefService.add(chartDef);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMsg.build(500,e.getMessage());
		}

		return ResponseMsg.build(0);
	}

	@RequestMapping("/list")
	@ResponseBody
	public TableJson list(HttpServletRequest request) {
		List<ChartDef> defList = chartDefService.pages(request);
		return TableJson.build(defList, chartDefService.countAll());
	}

	@Override
	public DaoService<ChartDef> injectService() {
		return chartDefService;
	}
}
